﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartSpace.Tasks
{
    public class TaskDbContext : DbContext
    {
        public TaskDbContext()
            : base("name=" + SmartSpace.Configuration.ConnectionStringName["task"])
        {
            if (SmartSpace.Configuration.IsAutomaticMigrations)
            {
                Database.SetInitializer<TaskDbContext>(new MigrateDatabaseToLatestVersion<TaskDbContext, DBMC>());
            }
            else
            {
                Database.SetInitializer<TaskDbContext>(null);
            }
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<TaskMessage>().ToTable("task_message");
            modelBuilder.Entity<TaskMessage>().HasKey(p => p.Id);

            base.OnModelCreating(modelBuilder);
        }
    }

    internal sealed class DBMC : DbMigrationsConfiguration<TaskDbContext>
    {
        public DBMC()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
        }
    }
}